package com.love.marriage.userBalanceInfo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.enums.TransactionType;
import com.love.marriage.common.interfaces.avoid.AvoidRepeatableCommit;
import com.love.marriage.common.interfaces.avoid.LogRestRequest;
import com.love.marriage.common.validator.Insert;
import com.love.marriage.common.validator.Update;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.orderInfo.service.PayInfoService;
import com.love.marriage.userBalanceInfo.service.UserBalanceInfoService;
import com.love.marriage.userBalanceInfo.vo.ResultUserBalanceInfoVo;
import com.love.marriage.userBalanceInfo.vo.ResultUserBalanceInfoVoPage;
import com.love.marriage.userBalanceInfo.vo.UserBalanceInfoAddVo;
import com.love.marriage.userBalanceInfo.vo.UserBalanceInfoVo;
import com.love.marriage.userMembershipInfo.vo.UserReduceMoneyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * description:  用户余额表restApi
 * author:       alex
 * createDate:   2024-10-17 21:19:44
 * version:      1.0.0
 */
@ApiSort(105)
@Tag(name = "用户余额表相关接口", description = "用户余额表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user-balance-info")
public class UserBalanceInfoController {

    private final UserBalanceInfoService userBalanceInfoService;

    private final PayInfoService payInfoService;

    @Value("${official.id}")
    private Long officialId;

    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "获取用户余额表分页", description = "获取用户余额表分页")
    @LogRestRequest(apiName = "获取用户余额表分页")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserBalanceInfoVoPage.class
                    )))
    @PostMapping(value = "/page")
    @Parameters({
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    public Result<Page<UserBalanceInfoVo>> getPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                                   @RequestParam(value = "pageSize", required = false) Long pageSize,
                                                   @RequestBody(required = false) UserBalanceInfoVo userBalanceInfoVo) {
        return Result.success(userBalanceInfoService.getPage(pageNum, pageSize, userBalanceInfoVo));
    }

    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "获取用户余额表详情", description = "获取用户余额表详情")
    @LogRestRequest(apiName = "获取用户余额表详情")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserBalanceInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "id", description = "id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping
    public Result<UserBalanceInfoVo> query(@RequestParam(value = "id") Long id) {
        return Result.success(userBalanceInfoService.queryUserBalanceInfo(id));
    }

    @ApiOperationSupport(order = 25, author = "alex")
    @Operation(summary = "获取用户余额", description = "获取用户余额")
    @LogRestRequest(apiName = "获取用户余额")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserBalanceInfoVo.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "userId", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @GetMapping(value = "queryBalance")
    @Tag(name = "用户", description = "用户信息")
    public Result<UserBalanceInfoVo> queryByUserId(@RequestParam(value = "userId") Long userId) {
        return Result.success(userBalanceInfoService.queryUserBalanceInfoByUserId(userId));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 26, author = "alex")
    @Operation(summary = "充值", description = "充值",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserBalanceInfoAddVo.class))
            ))
    @LogRestRequest(apiName = "充值")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = String.class)))
    @PostMapping(value = "addAmount")
    @Tag(name = "充值", description = "充值接口")
    public Result<WxPayUnifiedOrderV3Result.JsapiResult> addAmount(@RequestBody UserBalanceInfoAddVo userBalanceInfoAddVo) throws WxPayException {
        return Result.success(payInfoService.addAmount(userBalanceInfoAddVo));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 27, author = "alex")
    @Operation(summary = "打赏", description = "打赏",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserBalanceInfoAddVo.class))
            ))
    @LogRestRequest(apiName = "打赏")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserBalanceInfoVo.class)))
    @PostMapping(value = "reward")
    @Tag(name = "充值", description = "充值接口")
    public Result<UserBalanceInfoVo> reward(@RequestBody UserBalanceInfoAddVo userBalanceInfoAddVo) {
        return Result.success(userBalanceInfoService.reduceAmount(userBalanceInfoAddVo, officialId, TransactionType.REWARD.getCode(), "打赏"));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 29, author = "alex")
    @Operation(summary = "消费余额", description = "消费余额",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserBalanceInfoAddVo.class))
            ))
    @LogRestRequest(apiName = "消费余额")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserBalanceInfoVo.class)))
    @Parameters({
            @Parameter(name = "relationId", description = "关联id", required = false, schema = @Schema(implementation = Long.class, type = "Long", example = "1"))
    })
    @PostMapping(value = "reduceAmount")
    @Tag(name = "充值", description = "充值接口")
    public Result<UserBalanceInfoVo> reduceAmount(@RequestBody UserBalanceInfoAddVo userBalanceInfoAddVo,
                                                  @RequestParam(required = false) Long relationId) {
        return Result.success(userBalanceInfoService.reduceAmount(userBalanceInfoAddVo, relationId, TransactionType.REDUCE_RECHARGE.getCode(), "消费余额"));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 28, author = "alex")
    @Operation(summary = "消费现金", description = "消费现金",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserReduceMoneyVo.class))
            ))
    @LogRestRequest(apiName = "消费现金")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserBalanceInfoVo.class)))
    @PostMapping(value = "reduceMoney")
    @Tag(name = "充值", description = "充值接口")
    public Result<WxPayUnifiedOrderV3Result.JsapiResult> reduceMoney(@RequestBody UserReduceMoneyVo userReduceMoneyVo) throws Exception {
        return Result.success(userBalanceInfoService.reduceMoney(userReduceMoneyVo));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "新增用户余额表", description = "新增用户余额表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserBalanceInfoVo.class))
            ))
    @LogRestRequest(apiName = "新增用户余额表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserBalanceInfoVo.class)))
    @PostMapping
    public Result<UserBalanceInfoVo> add(@Validated({Insert.class}) @RequestBody UserBalanceInfoVo userBalanceInfoVo) {
        return Result.success(userBalanceInfoService.addUserBalanceInfo(userBalanceInfoVo));
    }

    @ApiOperationSupport(order = 40, author = "alex")
    @Operation(summary = "修改用户余额表", description = "修改用户余额表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "查询条件",
                    required = false,
                    content = @Content(schema = @Schema(implementation = UserBalanceInfoVo.class))
            ))
    @LogRestRequest(apiName = "修改用户余额表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserBalanceInfoVo.class)))
    @PutMapping
    public Result<UserBalanceInfoVo> update(@Validated({Update.class}) @RequestBody UserBalanceInfoVo userBalanceInfoVo) {
        return Result.success(userBalanceInfoService.updateUserBalanceInfo(userBalanceInfoVo));
    }

    @ApiOperationSupport(order = 50, author = "alex")
    @Operation(summary = "刪除用户余额表", description = "刪除用户余额表")
    @LogRestRequest(apiName = "刪除用户余额表")
    @Parameters({
            @Parameter(name = "ids", description = "ids,用，分割", required = true, schema = @Schema(implementation = String.class, type = "String", example = "1,2"))
    })
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultBoolean.class)))
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.success(userBalanceInfoService.deleteUserBalanceInfo(ids));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 60, author = "alex")
    @Operation(summary = "通过类型扣减余额", description = "通过类型扣减余额")
    @LogRestRequest(apiName = "通过类型扣减余额")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(schema = @Schema(implementation = ResultUserBalanceInfoVo.class)))
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = true, schema = @Schema(implementation = Long.class, type = "Long", example = "1")),
            @Parameter(name = "type", description = " membership_purchase： 会员购买\n" +
                    "recharge： 充值\n" +
                    "reduceRecharge： 消费余额\n" +
                    "reward： 打赏\n" +
                    "joinEvent： 参加活动\n" +
                    "startDating： 发起约会\n" +
                    "joinDating： 应邀\n" +
                    "commission： 回佣\n" +
                    "getPhone： 获取电话\n" +
                    "top： 置顶\n" +
                    "call： 通话\n" +
                    "chat： 聊天", required = true, schema = @Schema(implementation = String.class, type = "String", example = "getPhone"))
    })
    @PostMapping(value = "reduceBalanceByType")
    @Tag(name = "充值", description = "充值接口")
    public Result<Boolean> reduceBalanceByType(@RequestParam(value = "userId") Long userId,
                                               @RequestParam(value = "type") String type) throws Exception {
        return Result.success(userBalanceInfoService.reduceBalanceByType(userId, type));
    }
}
